﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Antlers
{
	public partial class MainWindow : Form
	{
		private ActiveView m_activeView;

		public MainWindow()
		{
			InitializeComponent();
			m_activeView = new ActiveView();
			m_activeView.ActiveFilesUpdated += new ActiveView.ActiveFilesUpdatedHandler(ui_thumbnailViewPanel.SetThumbnailList);
			Controls.Add(m_activeView);

			ui_directoryView.Init();
		}

		private void RefreshThumbView(string path)
		{
			m_activeView.SetDirectory(new DirectoryInfo(path), false);
		}

		#region Handlers

		private void ui_directoryView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			string path = ui_directoryView.ActivePath;
			RefreshThumbView(path);
		}

		private void ui_directoryView_AfterExpand(object sender, TreeViewEventArgs e)
		{
			ui_directoryView.Handler_AfterExpand(sender, e);
		}

		private void ui_keywordIntput_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == (char)Keys.Return)
			{
				if (m_activeView.AddKeywordFilter(ui_keywordInput.Text))
				{
					Button keywordButton = new Button();
					keywordButton.Text = ui_keywordInput.Text;
					keywordButton.Click += new EventHandler(Handler_KeywordButtonClick);
					ui_activeKeywordList.Controls.Add(keywordButton);
				}

				ui_keywordInput.Text = String.Empty;
				e.Handled = true;
			}
		}

		private void Handler_KeywordButtonClick(object sender, EventArgs mea)
		{
			Button button = (Button)sender;

			if(m_activeView.RemoveKeywordFilter(button.Text))
			{
				ui_activeKeywordList.Controls.Remove(button);
			}
		}

		private void Handler_SelectionChanged(IEnumerable<Thumbnail> selectedThumbnails)
		{
			ui_selectedKeywordList.UpdateList(selectedThumbnails);
		}

		#endregion // Handlers
	}
}
